package com.mapgis.service

import com.mapgis.constant.Constants
import com.mapgis.utils.{IOutil, JDBCUtil}
import javax.sql.rowset.serial.SerialClob

object LogToOracleService {

    /**
      * 将datax同步过程中的数据插入到Oracle中
      * @param tableNmae 目标表名
      * @param jobName 任务名
      * @param startTime datax任务的开始时间
      * @param endTime datax任务的结束时间
      * @param syncStatus datax的同步状态
      * @param dataxSyncLog datax同步的日志
      * @param dataxSyncNum 本次同步过来的数量
      */
    def dataxLogToOracle(jobName: String,
                         startTime: String,
                         endTime: String,
                         syncStatus: String,
                         dataxSyncLogPath: String,
                         dataxSyncNum: String): Unit ={
        var syncType=""
//        val dataxSyncLog=IOutil.readFile(dataxSyncLogPath)
//        val logClob = new SerialClob(dataxSyncLog.toCharArray)
//        println("dataX同步日志====================")
//        println(dataxSyncLog)
        var tableName=Constants.LOG_TABLE_REAL
        jobName match {
            case "building" => syncType="1"
            case "house" => syncType="2"
            case "resident" => syncType="3"
            case "residenthmt" => syncType="4"
            case "residentoutside" => syncType="5"
            case "residenthmtlogout" => syncType="6"
            case "residentoutsidelogout" => syncType="6"
            case "residentlogout" => syncType="6"
            case "enterprise" => syncType="7"
            case "event" => {
                syncType="11"
                tableName=Constants.LOG_TABLE_EVENT
            }
            case "enterprisemap" => {
                syncType="8"
                tableName=Constants.LOG_TABLE_ASSIST
            }
        }


        val sql =
            s"""
               |insert into ${tableName}
               |(
               |    sync_type,
               |    dx_sync_starttime,
               |    dx_sync_endtime,
               |    dx_sync_status,
               |    dx_sync_log,
               |    dx_sync_records,
               |    insert_time
               |)
               |values
               |(
               |    '${syncType}',
               |    to_date('${startTime}','yyyy-MM-dd hh24:mi:ss'),
               |    to_date('${endTime}','yyyy-MM-dd hh24:mi:ss'),
               |    '${syncStatus}',
               |    ?,
               |    ${dataxSyncNum},
               |    sysdate
               |)
             """.stripMargin
        println(sql)
        JDBCUtil.handExecuteClobUpdate(sql,dataxSyncLogPath)
    }

    /**
      * 在数据import到正式表的过程中的情况
      * @param tableNmae
      * @param jobName
      * @param startTime
      * @param endTime
      * @param importStatus
      * @param importErrorLog
      * @param importNum
      * @param dataxSyncStartTime
      */
    def importLogToOracle(jobName: String,
                          importStartTime: String,
                          importEndTime: String,
                          importStatus: String,
                          importErrorLogPath: String,
                          importNum: String,
                          dataxSyncStartTime: String): Unit ={
        var syncType=""
//        val importErrorLog=IOutil.readFile(importErrorLogPath)
        var tableName=Constants.LOG_TABLE_REAL
        jobName match {
            case "building" => syncType="1"
            case "house" => syncType="2"
            case "resident" => syncType="3"
            case "residenthmt" => syncType="4"
            case "residentoutside" => syncType="5"
            case "residenthmtlogout" => syncType="6"
            case "residentoutsidelogout" => syncType="6"
            case "residentlogout" => syncType="6"
            case "enterprise" => syncType="7"
            case "enterprisemap" => {
                syncType="8"
                tableName=Constants.LOG_TABLE_ASSIST
            }
        }
        val sql =
            s"""
               |update ${tableName}
               |set
               |    sync_import_starttime=to_date('${importStartTime}','yyyy-MM-dd hh24:mi:ss'),
               |    sync_import_endtime=to_date('${importEndTime}','yyyy-MM-dd hh24:mi:ss'),
               |    sync_import_status='${importStatus}',
               |    sync_import_log=?,
               |    sync_import_records='${importNum}',
               |    insert_time=sysdate
               |where
               |    sync_type='${syncType}'
               |    and dx_sync_starttime=to_date('${dataxSyncStartTime}','yyyy-MM-dd hh24:mi:ss')
             """.stripMargin
        println(sql)
        JDBCUtil.handExecuteClobUpdate(sql,importErrorLogPath)
    }

    /**
      * 事件解析过程中将日志等相关内容插入到数据库中
      * @param tableNmae
      * @param parseStartTime
      * @param parseEndTime
      * @param importTempTime
      * @param importPersonTime
      * @param parseState
      * @param parseErrorLog
      * @param importNum
      * @param dataxSyncStartTime
      */
    def eventParseLogToOracle(parseStartTime: String,
                              parseEndTime: String,
                              importTempTime: String,
                              importPersonTime:String,
                              parseState: String,
                              parseErrorLogPath: String,
                              succeedImportNum: String,
                              dataxSyncStartTime: String,
                              shouldImportNum: String
                             ): Unit ={
        var syncType="11"
//        val parseErrorLog=IOutil.readFile(parseErrorLogPath)
        val tableName=Constants.LOG_TABLE_EVENT
        val sql =
            s"""
               |update ${tableName}
               |set
               |    event_parse_starttime=to_date('${parseStartTime}','yyyy-MM-dd hh24:mi:ss'),
               |    event_parse_endtime=to_date('${parseEndTime}','yyyy-MM-dd hh24:mi:ss'),
               |    import_event_temp_time=to_date('${importTempTime}','yyyy-MM-dd hh24:mi:ss'),
               |    import_person_time=to_date('${importPersonTime}','yyyy-MM-dd hh24:mi:ss'),
               |    parse_state='${parseState}',
               |    parse_log=?,
               |    sync_import_records='${succeedImportNum}',
               |    insert_time=sysdate,
               |    dx_sync_records='${shouldImportNum}'
               |where
               |    sync_type='${syncType}'
               |    and dx_sync_starttime=to_date('${dataxSyncStartTime}','yyyy-MM-dd hh24:mi:ss')
             """.stripMargin
        println(sql)
        JDBCUtil.handExecuteClobUpdate(sql,parseErrorLogPath)
    }

}
